package cn.yudao.ymcc.service.impl;

import cn.yudao.ymcc.domain.*;
import cn.yudao.ymcc.dto.CourseSaveDto;
import cn.yudao.ymcc.dto.OnLineSmsDto;
import cn.yudao.ymcc.dto.UserInfoParamDto;
import cn.yudao.ymcc.feign.CourseFeign;
import cn.yudao.ymcc.mapper.CourseMapper;
import cn.yudao.ymcc.mq.producer.OnLineEmailProducer;
import cn.yudao.ymcc.mq.producer.OnLineSmsProducer;
import cn.yudao.ymcc.mq.producer.OnLineStationProducer;
import cn.yudao.ymcc.result.JSONResult;
import cn.yudao.ymcc.searchDoc.CourseDoc;
import cn.yudao.ymcc.service.*;
import cn.yudao.ymcc.utils.AssertUtil;
import cn.yudao.ymcc.utils.SecurityGetUserInfoUtil;
import cn.yudao.ymcc.vo.Course2OrderVo;
import cn.yudao.ymcc.vo.CourseDetailInfoVo;
import cn.yudao.ymcc.vo.CourseInfoVo;
import cn.yudao.ymcc.vo.RecommendedCourse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import cn.yudao.ymcc.feign.MediaFileFeign;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale.jia
 * @since 2022-08-07
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    private static final String COURSE_NOT_NULL = "课程名称重复";

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private ITeacherService teacherService;

    @Autowired
    private ICourseTeacherService courseTeacherService;

    @Autowired
    private ICourseDetailService detailService;

    @Autowired
    private ICourseMarketService marketService;

    @Autowired
    private ICourseResourceService resourceService;


    @Autowired
    private ICourseTypeService typeService;

    @Autowired
    private ICourseSummaryService summaryService;

    @Autowired
    private CourseFeign courseFeign;

    @Autowired
    private OnLineSmsProducer onLineSmsProducer;

    @Autowired
    private OnLineEmailProducer onLineEmailProducer;

    @Autowired
    private OnLineStationProducer onLineStationProducer;

    @Autowired
    private ICourseChapterService courseChapterService;

    @Autowired
    private MediaFileFeign mediaFileFeign;

    @Autowired
    private ICourseUserLearnService userLearnService;

    @Autowired
    private ICourseSummaryService courseSummaryService;

    /**
    * @Title: insert
    * @Description: 新增
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/11 17:43
    * @Parameters:
    * @Return
    */
    @Override
    public void insert(CourseSaveDto dto) {
        Course course = dto.getCourse();
        CourseDetail courseDetail = dto.getCourseDetail();
        CourseMarket courseMarket = dto.getCourseMarket();
        CourseResource courseResource = dto.getCourseResource();
        List<Long> teacharIds = dto.getTeacharIds();
        // 合法校验参数--校验课程是否已存在
        checkParameter(course);
        // 新增course表
        addCourse(course, teacharIds);
        // 新增详情表
        addCourseDetail(course, courseDetail);
        // 新增市场（金额）表t_course_market
        addCourseMarket(course, courseMarket);
        // 新增资源表
        addCourseResource(course, courseResource);
        // 新增summary表
        addCourseSummary(course);
        // 新增老师和课程的中间表
        addCourseAndTeacher(course, teacharIds);
        // 课程类型 + 1
        addCourseTypeNum(course);
    }

    /**
     * @Title: offLineCourse
     * @Description: 课程下架
     * @Author: Mr.Jia
     * @Version: 1.0
     * @Date:  2022/8/15 10:02
     * @Parameters:
     * @Return
     */
    @Override
    public void offLineCourse(Long courseId) {
        // 参数校验
        AssertUtil.isNotNull(courseId,"未点击课程");
        // 合法性校验 1 判断课程是否存在
        Course course = selectById(courseId);
        AssertUtil.isNotNull(course,"课程不存在");
        // 2 校验课程是否为上架状态
        AssertUtil.isEquals(Course.STATUS_ON_LINE.toString(),course.getStatus().toString(),"课程状态异常");
        // 修改参数状态
        course.setStatus(Course.STATUS_OFF_LINE);
        // 修改
        updateById(course);
        // 将数据从es中删除
        courseFeign.delete(courseId);
    }

    /**
    * @Title: detail
    * @Description: 课程详情页数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/17 9:54
    * @Parameters:
    * @Return
    */
    @Override
    public CourseDetailInfoVo detail(Long courseId) {
        // 非空校验
        AssertUtil.isNotNull(courseId,"非法参数");
        // 合法校验
        Course course = selectById(courseId);
        AssertUtil.isNotNull(course,"非法请求");
        // 校验课程是否上架
        AssertUtil.isTrue(course.getStatus().intValue() == Course.STATUS_ON_LINE,"非法请求");
        // 查询课程相关数据
        CourseDetail courseDetail = detailService.selectById(courseId);
        CourseMarket courseMarket = marketService.selectById(course);
        // 查询课程对应的老师
        Wrapper<CourseTeacher> teacherWraper = new EntityWrapper<>();
        teacherWraper.eq("course_id",courseId);
        List<CourseTeacher> courseTeachers = courseTeacherService.selectList(teacherWraper);
        // 得到老师的id集合
        List<Long> teacherIds = courseTeachers.stream()
                .map(courseTeacher -> courseTeacher.getTeacherId())
                .collect(Collectors.toList());
        // 查询所有老师
        List<Teacher> teachers = teacherService.selectBatchIds(teacherIds);
        // 查询所有的章节
        Wrapper<CourseChapter> query = new EntityWrapper<>();
        query.eq("course_id",courseId);
        List<CourseChapter> courseChapters = courseChapterService.selectList(query);
        // 查询课程对应的所有媒体视频
        JSONResult jsonResult = mediaFileFeign.loadMediaByCourseId(courseId);
        // 判断数据是否存在
        AssertUtil.isNotNull(jsonResult.getData(),"视频加载异常");
        // 判断是否进了熔断
        AssertUtil.isTrue(jsonResult.isSuccess(),jsonResult.getMessage());
        // 将媒体数据转换为list集合
        List<MediaFile> mediaFiles = JSON.parseArray(jsonResult.getData().toString(), MediaFile.class);
        // 将视频放到对应的章节
        // 1.我们先将章节转换为map对象存储，便于查询使用
        Map<Long, CourseChapter> courseChapterMap = courseChapters.stream().collect(Collectors.toMap(CourseChapter::getId, courseChapter -> courseChapter));
        // 2.我们将media放到对应的章节中去
        List<CourseChapter> returnCourseChapter = mediaFiles.stream().map(mediaFile -> {
            CourseChapter courseChapter = courseChapterMap.get(mediaFile.getChapterId()); // 得到视频对应的章节
            courseChapter.getMediaFiles().add(mediaFile); // 将视频放到对应的章节下
            return courseChapter;
        }).collect(Collectors.toList());
        // 将数据装入CourseDetailInfoVo中
        CourseDetailInfoVo courseDetailInfoVo = new CourseDetailInfoVo();
        courseDetailInfoVo.setCourse(course);
        courseDetailInfoVo.setCourseDetail(courseDetail);
        courseDetailInfoVo.setCourseChapters(returnCourseChapter);
        courseDetailInfoVo.setCourseMarket(courseMarket);
        courseDetailInfoVo.setTeachers(teachers);
        return courseDetailInfoVo;
    }

    /**
    * @Title: getForUser
    * @Description: 根据mediaId查询到当前登录用户是否拥有这个课程的媒体资源数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/17 15:47
    * @Parameters:
    * @Return
    */
    @Override
    public String getForUser(Long mediaId) {
        // 非空校验
        AssertUtil.isNotNull(mediaId,"非法请求");
        // 根据mediaId查询到对应的Media对象
        JSONResult mediaById = mediaFileFeign.getMediaById(mediaId);
        // 判断对象是否存在
        AssertUtil.isNotNull(mediaById.getData(),"参数非法");
        String data = mediaById.getData().toString();
        // 判断media对象的状态
        MediaFile mediaFile = JSONObject.parseObject(data, MediaFile.class);
        // 判断视频状态
        AssertUtil.isTrue(mediaFile.getFileStatus().intValue() == MediaFile.STATUES_SUCCESS,"视频状态异常");
        // 判断视频是否免费
        if (mediaFile.getFree()){
            return mediaFile.getFileUrl();
        }
        // 判断用户是否拥有此视频对应的课程
        // 通过自定义的方法获取userInfoParamDto对象得到security上下文对象中的用户信息
        UserInfoParamDto userInfoParamDto = SecurityGetUserInfoUtil.getUserInfoParamDto();
        Long LoginId = userInfoParamDto.getId();
        // 根据loginId查询数据库查询当前用户拥有的课程
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",LoginId);
        wrapper.eq("course_id",mediaFile.getCourseId());
        CourseUserLearn courseUserLearn1 = userLearnService.selectOne(wrapper);
//        List<CourseUserLearn> courseUserLearns = userLearnService.selectList(wrapper);
        // 判断用户是否拥有课程
        AssertUtil.isNotNull(courseUserLearn1,"您未拥有课程");
        // 获取到当前用户拥有的所有课程的id
//        List<Long> collectIds = courseUserLearns.stream()
//                .map(courseUserLearn -> courseUserLearn.getCourseId())
//                .collect(Collectors.toList());
//        Long count = collectIds.stream()
//                .filter(courseId -> courseId == mediaFile.getCourseId())
//                .count();
        // 校验是否具有当前课程
//        AssertUtil.isNotNull(count,"您未拥有当前课程");
        String fileUrl = mediaFile.getFileUrl();
        return fileUrl;
    }

    /**
    * @Title: courseOrderInfo
    * @Description: 获取订单详情页数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/17 18:04
    * @Parameters:
    * @Return
    */
    @Override
    public CourseInfoVo courseOrderInfo(String courseIds) {
        // 非空校验
        AssertUtil.isNotEmpty(courseIds,"非法请求");
        // 获取购买的课程的ids
        String[] split = courseIds.split(",");
        List<Long> ids = Arrays.asList(split)
                .stream()
                .map(id -> Long.valueOf(id))
                .collect(Collectors.toList());
        // 获取到要购买的课程
        List<Course> courses = selectBatchIds(ids);
        // 获取到要购买课程的Market信息
        List<CourseMarket> courseMarkets = marketService.selectBatchIds(ids);
        // 把courseMarkets转换为map
        Map<Long, CourseMarket> marketMap = courseMarkets.stream()
                .collect(Collectors.toMap(CourseMarket::getId, courseMarket -> courseMarket));
        // 将数据填充到Course2OrderVo中,将Course2OrderVo填充到CourseInfoVo集合中
         final BigDecimal[] totalAmount = {new BigDecimal(0.00)}; // 创建一个金额对象
        List<Course2OrderVo> course2OrderVos = courses.stream().map(course -> {
            // 校验课程是否上架
            AssertUtil.isTrue(course.getStatus() == Course.STATUS_ON_LINE,"未知课程");
            // 填充数据到Course2OrderVo
            Course2OrderVo course2OrderVo = new Course2OrderVo();
            course2OrderVo.setCourse(course);
            CourseMarket courseMarket = marketMap.get(course.getId());
            course2OrderVo.setCourseMarket(courseMarket);
            totalAmount[0] = totalAmount[0].add(courseMarket.getPrice());// 每个课程金额相加
            return course2OrderVo;
        }).collect(Collectors.toList());
        return new CourseInfoVo(course2OrderVos, totalAmount[0]);
    }

    /**
    * @Title: getCourseByIds
    * @Description: 通过课程ids查询课程和课程销售表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/18 13:17
    * @Parameters:
    * @Return
    */
    @Override
    public CourseInfoVo getCourseByIds(List<Long> ids) {
        List<Course> courses = selectBatchIds(ids);
        List<CourseMarket> courseMarkets = marketService.selectBatchIds(ids);
        // 把courseMarkets转换为map
        Map<Long, CourseMarket> marketMap = courseMarkets.stream()
                .collect(Collectors.toMap(CourseMarket::getId, courseMarket -> courseMarket));
        // 将数据填充到Course2OrderVo中,将Course2OrderVo填充到CourseInfoVo集合中
        final BigDecimal[] totalAmount = {new BigDecimal(0.00)}; // 创建一个金额对象
        List<Course2OrderVo> course2OrderVos = courses.stream().map(course -> {
            // 校验课程是否上架
            AssertUtil.isTrue(course.getStatus() == Course.STATUS_ON_LINE,"未知课程");
            // 填充数据到Course2OrderVo
            Course2OrderVo course2OrderVo = new Course2OrderVo();
            course2OrderVo.setCourse(course);
            CourseMarket courseMarket = marketMap.get(course.getId());
            course2OrderVo.setCourseMarket(courseMarket);
            totalAmount[0] = totalAmount[0].add(courseMarket.getPrice());// 每个课程金额相加
            return course2OrderVo;
        }).collect(Collectors.toList());
        return new CourseInfoVo(course2OrderVos, totalAmount[0]);
    }

    /**
    * @Title: recommendedCourse
    * @Description: 课程推荐
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/9/7 18:47
    * @Parameters:
    * @Return
    */
    @Override
    public void recommendedCourse(Long courseId) {
        // 查询数据库
        Course course = selectById(courseId);
        CourseSummary courseSummary = courseSummaryService.selectById(courseId);
        // 判断课程是否存在
        AssertUtil.isNotNull(course,"所选课程不存在");
        AssertUtil.isNotNull(courseSummary,"所选课程不存在");
        // 判断课程是否上架
        AssertUtil.isTrue(course.getStatus() == Course.STATUS_ON_LINE,"课程未上架");
        // 讲course的基本数据存储到RecommendedCourse中
        RecommendedCourse recommendedCourse = new RecommendedCourse();
        recommendedCourse.setId(course.getId());
        recommendedCourse.setPic(course.getPic());
        recommendedCourse.setViewCount(courseSummary.getViewCount());
        // 查讯redis中是否已有推荐课程
        Object recommendedCourse1 = redisTemplate.opsForValue().get("RecommendedCourse");
        List<RecommendedCourse> recommendedCourses = new ArrayList<>();
        if (recommendedCourse1 != null){
            redisTemplate.delete("RecommendedCourse"); // 先清空
            // 获取到集合中的数据
            recommendedCourses = JSONObject.parseArray(recommendedCourse1.toString(),RecommendedCourse.class);
        }
        // 添加新推荐的数据到集合中
        recommendedCourses.add(recommendedCourse);
        // 再次存入redis中
        redisTemplate.opsForValue().set("RecommendedCourse",JSON.toJSONString(recommendedCourses));
    }

    /**
    * @Title: getRecommendedCourses
    * @Description: 获取推荐的数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/9/7 19:11
    * @Parameters:
    * @Return
    */
    @Override
    public List<RecommendedCourse> getRecommendedCourses() {
        Object recommendedCourse = redisTemplate.opsForValue().get("RecommendedCourse");
        // 非空判断
        if (recommendedCourse != null){
            // 类型转换
            List<RecommendedCourse> recommendedCourses = JSONObject.parseArray(recommendedCourse.toString(),RecommendedCourse.class);
            // 判断推荐条数是否有五条
            if (recommendedCourses.size() <= 5){
                return recommendedCourses;
            }
            // 根据推荐量排序得到前五条数据
            Collections.sort(recommendedCourses, new Comparator<RecommendedCourse>() {
                @Override
                public int compare(RecommendedCourse o1, RecommendedCourse o2) {
                    return o1.getViewCount() - o2.getViewCount();
                }
            });
            List<RecommendedCourse> recommendedCoursesView = recommendedCourses.subList(0, 5);
            // 存入redis中并设置1h有效
            redisTemplate.opsForValue().set("RecommendedCourseFirst", JSON.toJSONString(recommendedCoursesView),1*60*60, TimeUnit.SECONDS);
            return recommendedCoursesView;
        }


        return null;
    }

    /**
    * @Title: onLineCourse
    * @Description: 课程上架
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/14 12:44
    * @Parameters:
    * @Return
    */
    @Override
    @Transactional
    public void onLineCourse(Long courseId) {
        // 参数校验
        AssertUtil.isNotNull(courseId,"未点击课程");
        // 合法性校验 1 判断课程是否存在
        Course course = selectById(courseId);
        AssertUtil.isNotNull(course,"课程不存在");
        // 2 校验课程是否为下架状态
        AssertUtil.isEquals(Course.STATUS_OFF_LINE.toString(),course.getStatus().toString(),"课程状态异常");
        // 修改参数状态
        course.setStatus(Course.STATUS_ON_LINE);
        // 修改发布时间
        Date date = new Date();
        course.setOnlineTime(date);
        // 修改
        updateById(course);
        // 添加数据 -- 查询相关数据
        CourseDoc courseDoc = new CourseDoc();
        CourseSummary courseSummary = summaryService.selectById(courseId);
        CourseMarket courseMarket = marketService.selectById(courseId);
        CourseType courseType = typeService.selectById(course.getCourseTypeId());
        // 添加数据 -- 数据赋值
        BeanUtils.copyProperties(courseSummary,courseDoc);
        BeanUtils.copyProperties(courseMarket,courseDoc);
        BeanUtils.copyProperties(course,courseDoc);
        courseDoc.setCourseTypeName(courseType.getName());
        courseDoc.setId(courseId);
        courseDoc.setChargeName(courseMarket.getCharge().intValue() == 1?"免费":"收费");
        // 将数据传输到es中
        JSONResult save = courseFeign.save(courseDoc);
        AssertUtil.isTrue(save.isSuccess(),"课程保存失败");

        // 调用生产者发送短信信息
        boolean phone = senOnLineSms(courseDoc);
        AssertUtil.isTrue(phone,"新品课程短信推荐失败");
        // 调用生产者发送邮件
        boolean email = senOnLineEmail(courseDoc);
        AssertUtil.isTrue(email,"新品课程邮件推荐失败");
        // 调用生产者发送站内信息
        boolean sation = senOnLineSation(courseDoc);
        AssertUtil.isTrue(sation,"新品课程站内信推荐失败");

    }



    /**
    * @Title: senOnLineSms
    * @Description: 调用生产者发送短信
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/14 17:13
    * @Parameters:
    * @Return
    */
    public boolean senOnLineSms(CourseDoc courseDoc){
        // 查询所有的前台用户
        User user = new User();
        user.setId(99L);
        user.setPhone("000000000000000000");

        OnLineSmsDto onLineSmsDto = new OnLineSmsDto();
        onLineSmsDto.setUsers(new ArrayList<User>());
        onLineSmsDto.getUsers().add(user);
        onLineSmsDto.setCourseDoc(courseDoc);
        boolean success = onLineSmsProducer.sendProductMessage(onLineSmsDto);

        return success;
    }


    /**
    * @Title: senOnLineEmail
    * @Description: 调用生产者发送邮件
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/14 20:19
    * @Parameters:
    * @Return
    */
    public boolean senOnLineEmail(CourseDoc courseDoc){
        // 查询所有的前台用户
        User user = new User();
        user.setId(99L);
        user.setPhone("000000000000000000");
        user.setEmail("398991015@qq.com");
        OnLineSmsDto onLineSmsDto = new OnLineSmsDto();
        onLineSmsDto.setUsers(new ArrayList<User>());
        onLineSmsDto.getUsers().add(user);
        onLineSmsDto.setCourseDoc(courseDoc);
        boolean success = onLineEmailProducer.sendProductMessage(onLineSmsDto);

        return success;
    }

    /**
    * @Title: senOnLineSation
    * @Description: 调用生产者发送站内信息
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/14 20:18
    * @Parameters:
    * @Return
    */
    public boolean senOnLineSation(CourseDoc courseDoc){
        // 查询所有的前台用户
        User user = new User();
        user.setId(99L);
        user.setPhone("000000000000000000");

        OnLineSmsDto onLineSmsDto = new OnLineSmsDto();
        onLineSmsDto.setUsers(new ArrayList<User>());
        onLineSmsDto.getUsers().add(user);
        onLineSmsDto.setCourseDoc(courseDoc);
        boolean success = onLineStationProducer.sendProductMessage(onLineSmsDto);

        return success;
    }

    /**
    * @Title: checkParameter
    * @Description: 合法校验参数--校验课程是否已存在
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/11 18:51
    * @Parameters:
    * @Return
    */
    private void checkParameter(Course course) {
        EntityWrapper<Course> Wrapper = new EntityWrapper<>();
        Wrapper.eq("name", course.getName());
        Course courseTmp = selectOne(Wrapper);
        AssertUtil.isNull(courseTmp,COURSE_NOT_NULL);
    }

    /** 
    * @Title: addCourse
    * @Description: 新增course表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/11 18:49  
    * @Parameters: 
    * @Return 
    */
    private void addCourse(Course course, List<Long> teacharIds) {
        course.setStatus(Course.STATUS_OFF_LINE);
        // 通过自定义的方法获取userInfoParamDto对象得到security上下文对象中的用户信息
        UserInfoParamDto userInfoParamDto = SecurityGetUserInfoUtil.getUserInfoParamDto();

        Long loginId = userInfoParamDto.getId();
        String loginName = userInfoParamDto.getUsername();
        course.setLoginId(loginId);
        course.setLoginUserName(loginName);
        List<Teacher> teachers = teacherService.selectBatchIds(teacharIds);
        String teacherName = teachers.stream().map(teacher -> teacher.getName()).collect(Collectors.joining(",")).toString();
        course.setTeacherNames(teacherName);
        insert(course);
    }

    /** 
    * @Title: addCourseDetail
    * @Description: 新增详情表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/11 18:49  
    * @Parameters: 
    * @Return 
    */
    private void addCourseDetail(Course course, CourseDetail courseDetail) {
        courseDetail.setId(course.getId());
        detailService.insert(courseDetail);
    }

    /** 
    * @Title: addCourseMarket
    * @Description: 新增t_course_market表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/11 18:48  
    * @Parameters: 
    * @Return 
    */
    private void addCourseMarket(Course course, CourseMarket courseMarket) {
        courseMarket.setId(course.getId());
        marketService.insert(courseMarket);
    }

    /** 
    * @Title: addCourseResource
    * @Description: 新增资源表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/11 18:48  
    * @Parameters: 
    * @Return 
    */
    private void addCourseResource(Course course, CourseResource courseResource) {
        courseResource.setCourseId(course.getId());
        resourceService.insert(courseResource);
    }

    /** 
    * @Title: addCourseSummary
    * @Description: 新增summary表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/11 18:48  
    * @Parameters: 
    * @Return 
    */
    private void addCourseSummary(Course course) {
        CourseSummary courseSummary = new CourseSummary();
        courseSummary.setId(course.getId());
        summaryService.insert(courseSummary);
    }

    /** 
    * @Title: addCourseAndTeacher
    * @Description: 新增老师和课程的中间表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/11 18:47  
    * @Parameters: 
    * @Return 
    */
    private void addCourseAndTeacher(Course course, List<Long> teacharIds) {
        List<CourseTeacher> collect = teacharIds.stream().map(tId -> {
            CourseTeacher courseTeacher = new CourseTeacher();
            courseTeacher.setTeacherId(tId);
            courseTeacher.setCourseId(course.getId());
            return courseTeacher;
        }).collect(Collectors.toList());
        courseTeacherService.insertBatch(collect);
    }

    /**
    * @Title: addCourseTypeNum
    * @Description: 当前添加的课程所属类型的数量加1
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/11 18:46
    * @Parameters:
    * @Return
    */
    private void addCourseTypeNum(Course course) {
        EntityWrapper<CourseType> WrapperType = new EntityWrapper<>();
        WrapperType.eq("id", course.getCourseTypeId());
        CourseType courseType = typeService.selectOne(WrapperType);
        courseType.setTotalCount(courseType.getTotalCount() + 1);
        typeService.update(courseType,WrapperType);
    }
}
